#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Calculate Crystal Tilt from Carbon Tilt                            #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 11/20/2007                                             #
# Last Modification: 11/20/2007                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 33
#
# MANUAL: This script calculates the sample tilt geometry (described in TLTAXA, TAXA and TANGL) from the support film tilt geometry (which is described in TLTAXIS and TLTANG).
#
# MANUAL: It only translates one set of numbers (TLTAXIS and TLTANG, plus some additional info like magnification,  etc.) into another set of numbers (TAXA and TANGL, and also TLTAXA).
#
# MANUAL: You can run this script after having determined the tilt geometry of the carbon film support, to determine the orientation of the 2D crystal on that support.
#
#
# PUBLICATION: 2dx - User-friendly image processing for 2D crystals: <A HREF="http://dx.doi.org/10.1016/j.jsb.2006.07.020">J. Struct. Biol. 157 (2007) 64-72</A>
# PUBLICATION: Image processing of 2D crystal images: <A HREF="http://link.springer.com/protocol/10.1007%2F978-1-62703-176-9_10">Methods in Molecular Biology (2012)</A>
#
# DISPLAY: TLTAXIS
# DISPLAY: TLTANG
# DISPLAY: TLTAXA
# DISPLAY: TAXA
# DISPLAY: TANGL
# DISPLAY: det_tilt
# DISPLAY: lattice
# DISPLAY: secondlattice
# DISPLAY: backuplattice
# DISPLAY: comment
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
#
set SYN_Unbending = ""
set PHASEORI_done = ""
#
set imagename = ""
set nonmaskimagename = ""
set imagesidelength = ""
set lattice = ""
set magnification = ""
set stepdigitizer = ""
set CS = ""
set KV = ""
set defocus = ""
set RESMIN = ""
set RESMAX = ""
set phacon = ""
set det_defocus = ""
set det_tilt = ""
set TLTAXIS = ""
set TLTANG = ""
set TLTAXA = ""
set TAXA = ""
set TANGL = ""
#
#$end_vars
#
set scriptname = 2dx_getSampleTilt
#
\rm -f LOGS/${scriptname}.results
#
echo "<<@evaluate>>"
#
set IS_2DX = yes
source ${proc_2dx}/initialize
#
if ( x${TLTAXIS} == "x-" || x${TLTAXIS} == "x--" ) then
  set TLTAXIS = "0.0"
endif
if ( x${TLTANG} == "x-" || x${TLTANG} == "x--" ) then
  set TLTANG = "0.0"
endif
if ( x${TLTAXA} == "x-" || x${TLTAXA} == "x--" ) then
  set TLTAXA = "0.0"
endif
if ( x${TAXA} == "x-" || x${TAXA} == "x--" ) then
  set TAXA = "0.0"
endif
if ( x${TANGL} == "x-" || x${TANGL} == "x--" ) then
  set TANGL = "0.0"
endif
#
if ( (${SYN_Unbending} != "0") && (${PHASEORI_done} == "y")) then
  ${proc_2dx}/linblock "Skipping. Synthetical Reference is used"
  ${proc_2dx}/linblock "Defocus should be determined already."
  #
  echo "# IMAGE-IMPORTANT: FFTIR/${imagename}_fft.mrc <FFT of Image>" >> LOGS/${scriptname}.results
  echo "# IMAGE-IMPORTANT: FFTIR/${imagename}_red_fft.mrc <FFT of Downsampled Image>" >> LOGS/${scriptname}.results
  #
  exit
  ##########################################################################
  ${proc_2dx}/linblock "${scriptname} - normal end."
  ##########################################################################
endif
#
echo nonmaskimagename = ${nonmaskimagename}
echo imagesidelength = ${imagesidelength}
echo magnification = ${magnification}
echo stepdigitizer = ${stepdigitizer}
echo CS = ${CS}
echo KV = ${KV}
echo defocus = ${defocus}
echo RESMIN = ${RESMIN}
echo RESMAX = ${RESMAX}
echo phacon = ${phacon}
echo det_tilt = ${det_tilt}
#
echo "<<@progress: 1>>"
#
set date = `date`
echo date = ${date}
#
#set smallimagesidelength=2048
set smallimagesidelength=`echo ${imagesidelength} | awk '{if($1<2049) {s=512} else if ($1<4099) {s=1024} else {s=2048}} END {print s}'`
echo smallimagesidelength=${smallimagesidelength}
#
echo "# IMAGE-IMPORTANT: FFTIR/${imagename}_fft.mrc <FFT of Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: FFTIR/${imagename}_red_fft.mrc <FFT of Downsampled Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${imagename}.mrc <Image>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: FFTIR/${imagename}_red.mrc <Downsampled Image>" >> LOGS/${scriptname}.results
#
#
set oldlat = `echo ${lattice}`
set lattice = `echo ${lattice} | sed 's/ /,/g' | sed 's/,,,/,/g' | sed 's/,,/,/g'`
if ( "${oldlat}" != ${lattice} ) then
  echo "set lattice = ${lattice}" >> LOGS/${scriptname}.results
  echo "lattice corrected from ${oldlat} to ${lattice}"
endif
#
set inimage = ${nonmaskimagename}.mrc
#
echo "<<@progress: 20>>"
#
if ( ${det_tilt} == 'y' ) then
  #
  #############################################################################
  ${proc_2dx}/linblock "2dx_tiltgeom2: Determine other tilt angles."
  #############################################################################
  #
  if ( ${lattice} != '0.0,0.0,0.0,0.0' ) then
    #
    ${bin_2dx}/2dx_tiltgeom2.exe << eot
${lattice}
${TLTAXIS}
${TLTANG}
eot
    # 
    ${proc_2dx}/lin "="
    cat 2dx_tiltgeom2.out
    source 2dx_tiltgeom2.out
    \rm -f 2dx_tiltgeom2.out
    ${proc_2dx}/lin "="
    #
  else
    ${proc_2dx}/linblock "ERROR: Lattice not yet defined."
    ${proc_2dx}/protest "First determine  lattice."
  endif
  #
  # set highangle = `echo ${TLTANG} | awk '{if($1>25 || $1<-25) {s=1} else {s=0}} END {print s}'`
  #
  # if ( ${highangle} == '1' ) then
    # Only with higher tilt angle, the sample tilt is taken from the lattice distortion. 
    # At lower tilts, the sample tilt was more reliably determined by the defocus gradient in other scripts.
    #
    #############################################################################
    ${proc_2dx}/linblock "Saving tilt geometry from lattice, because of high tilt."
    #############################################################################
    #
    set TLTAXIS = `echo ${TLTAXIS} | awk '{s=$1} END {printf "%.3f", s}'`
    set TLTANG  = `echo ${TLTANG}  | awk '{s=$1} END {printf "%.3f", s}'`
    set TLTAXA  = `echo ${TLTAXA}  | awk '{s=$1} END {printf "%.3f", s}'`
    set TAXA    = `echo ${TAXA}    | awk '{s=$1} END {printf "%.3f", s}'`
    set TANGL   = `echo ${TANGL}   | awk '{s=$1} END {printf "%.3f", s}'`
    #
    echo "set TLTAXIS = ${TLTAXIS}" >> LOGS/${scriptname}.results
    echo "set TLTANG = ${TLTANG}" >> LOGS/${scriptname}.results
    echo "set TLTAXA = ${TLTAXA}" >> LOGS/${scriptname}.results
    echo "set TAXA = ${TAXA}" >> LOGS/${scriptname}.results
    echo "set TANGL = ${TANGL}" >> LOGS/${scriptname}.results 
    #
    echo "set DEFOCUS_TLTAXA = ${TLTAXA}" >> LOGS/${scriptname}.results
    echo "set DEFOCUS_TAXA = ${TAXA}" >> LOGS/${scriptname}.results
    echo "set DEFOCUS_TANGL = ${TANGL}" >> LOGS/${scriptname}.results
    #
    echo " " >> History.dat
    echo "::Sample tilt from support tilt:" >> History.dat
    echo "::TLTAXIS = ${TLTAXIS}" >> History.dat
    echo "::TLTANG  = ${TLTANG}" >> History.dat
    echo "::TLTAXA  = ${TLTAXA}" >> History.dat
    echo "::TAXA    = ${TAXA}" >> History.dat
    echo "::TANGL   = ${TANGL}" >> History.dat
    #
  # else
  #  #############################################################################
  #  ${proc_2dx}/linblock "Not saving tilt geometry from lattice, because of low tilt."
  #  #############################################################################
  # endif
  #
else
  #
  ${proc_2dx}/linblock "Nothing done here."
  #
endif
#
echo "<<@progress: 80>>"
#
echo "set CTF_done = n" >> LOGS/${scriptname}.results
echo "set MERGING_done = n" >> LOGS/${scriptname}.results
#
echo "<<@progress: 100>>"
#
##########################################################################
${proc_2dx}/linblock "${scriptname} - normal end."
##########################################################################
#
#
#
